//
//  HPShareDetailsTableViewCell.m
//  fruitsDealers
//
//  Created by sgone on 16/4/19.
//  Copyright © 2016年 com.sgone. All rights reserved.
//

//#import <CoreText/CoreText.h>
//#import <UIKit/UIKit.h>

#import "HPShareDetailsTableViewCell.h"
#import "Common.h"
#import "labelAdaptive.h"
#import "UIView+Helper.h"

//#import "NSString+WPAttributedMarkup.h"
//
//#import "WPAttributedStyleAction.h"
//#import "WPHotspotLabel.h"

//#import "MLLinkLabel.h"

@interface HPShareDetailsTableViewCell ()  {
    
//    <MLLinkLabelDelegate>
}

/**
 评论内容
 */
//@property (nonatomic, strong) WPHotspotLabel *commentLabel;

//@property (nonatomic, strong) MLLinkLabel *commentLabel;

@property (nonatomic, strong) UILabel *commentLabel;

@end

@implementation HPShareDetailsTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    static NSString *ID = @"HPShareDetailsTableViewCell";
    HPShareDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HPShareDetailsTableViewCell" owner:nil options:nil] objectAtIndex:0];
    }
    
    
    
  
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (CGFloat)cellHeight {
    
    _commentLabel = [[UILabel alloc] init];
    _commentLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    _commentLabel.numberOfLines = 0;
    _commentLabel.textColor = FDFont_COLOR;
    
    [self addSubview:_commentLabel];

    
    
    NSMutableAttributedString *contentTextStr = nil;
    
    if ([checkNull(_messageDic[@"pusername"]) length] == 0) {
        
        NSString *userName;
        if ([checkNull(_messageDic[@"username"]) length] == 0) {
            
            userName = @"";
        } else {
            
            userName = _messageDic[@"username"];
        }
        
        NSString *fatherStr = [NSString stringWithFormat:@"%@：%@",userName,_messageDic[@"discuss"]];
        NSString *subStr = userName;
        
        contentTextStr = [self getTextByString:fatherStr string:subStr];
        
    } else {
        
        NSString *userName;
        if ([checkNull(_messageDic[@"username"]) length] == 0) {
            
            userName = @"";
        } else {
            
            userName = _messageDic[@"username"];
        }
        
        NSString *firstFatherStr = [NSString stringWithFormat:@"%@ 回复 ",userName];
        NSString *firstSubStr = userName;
        
        contentTextStr = [self getTextByString:firstFatherStr string:firstSubStr];
        
        NSString *secondFatherStr = [NSString stringWithFormat:@"%@：%@",_messageDic[@"pusername"],_messageDic[@"discuss"]];
        NSString *secondSubStr = _messageDic[@"pusername"];
        
        NSMutableAttributedString *contentStr = [self getTextByString:secondFatherStr string:secondSubStr];
        
        [contentTextStr appendAttributedString:contentStr];
    }
    
    
    _commentLabel.attributedText = contentTextStr;
    
//    self.commentLabel = [[WPHotspotLabel alloc] initWithFrame:CGRectMake(0, 0, FDSCREEN_WIDTH, 25 / FDLaOt)];
//    self.commentLabel.numberOfLines = 0;
//    
//    [self addSubview:_commentLabel];
//    
//    if (![[_messageDic[@"p_user_name"] allKeys] containsObject:@"shopName"]) {
//        
//        NSDictionary *style = @{@"body":[UIFont systemFontOfSize:14 / FDLaOt],
//                                 @"help":[WPAttributedStyleAction styledActionWithAction:^{
//                                     
//                                     [self userCommentClick:@{@"name":_messageDic[@"user_name"][@"shopName"],@"id":_messageDic[@"user_name"][@"wId"]}];
//                                 }],
//                                 @"link": FDUSERNAME_COLOR};
//        
//        self.commentLabel.attributedText = [[NSString stringWithFormat:@"<help>%@</help>：%@",_messageDic[@"user_name"][@"shopName"],_messageDic[@"discuss"]] attributedStringWithStyleBook:style];
//        
//    } else {
//        
//        NSDictionary *style = @{@"body":[UIFont systemFontOfSize:14.0 / FDLaOt],
//                                @"help":[WPAttributedStyleAction styledActionWithAction:^{
//                                    
//                                    [self userCommentClick:@{@"name":_messageDic[@"user_name"][@"shopName"],@"id":_messageDic[@"user_name"][@"wId"]}];
//                                }],
//                                @"settings":[WPAttributedStyleAction styledActionWithAction:^{
//                                    
//                                    [self userCommentClick:@{@"name":_messageDic[@"p_user_name"][@"shopName"],@"id":_messageDic[@"p_user_name"][@"wId"]}];
//                                }],
//                                @"link": FDUSERNAME_COLOR};
//        
//        self.commentLabel.attributedText = [[NSString stringWithFormat:@"<help>%@</help> 回复 <settings>%@</settings>：%@",_messageDic[@"user_name"][@"shopName"],_messageDic[@"p_user_name"][@"shopName"],_messageDic[@"discuss"]] attributedStringWithStyleBook:style];
//    }
    
    
    
    
//    _commentLabel = [MLLinkLabel new];
//    _commentLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
//    _commentLabel.linkTextAttributes = @{NSForegroundColorAttributeName : FDUSERNAME_COLOR};
//    _commentLabel.delegate = self;
//    _commentLabel.numberOfLines = 0;
//    _commentLabel.textColor = FDFont_COLOR;
//    
//    _commentLabel.attributedText = [self generateAttributedStringWithFirstUserName:_messageDic[@"username"] secondUserName:_messageDic[@"pusername"] contentStr:_messageDic[@"discuss"]];
//    
//    [self addSubview:_commentLabel];
    
    CGFloat X = 8;
    CGFloat Y = 4;
    CGSize commentLabelSize = [labelAdaptive sizeWithString:_commentLabel.text font:_commentLabel.font];
    
    _commentLabel.frame = CGRectMake(X, Y, ViewWidth - X * 2, commentLabelSize.height);
    
    return _commentLabel.height + Y * 2;
}

- (NSMutableAttributedString *)generateAttributedStringWithFirstUserName:(NSString *)firstUserName secondUserName:(NSString *)secondUserName contentStr:(NSString *)contentStr
{
//    NSString *text = firstUserName;
//    if ([checkNull(secondUserName) length] != 0) {
//        text = [text stringByAppendingString:[NSString stringWithFormat:@" 回复 %@", secondUserName]];
//    }
//    text = [text stringByAppendingString:[NSString stringWithFormat:@"：%@", contentStr]];
//    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
//    if ([checkNull(secondUserName) length] != 0) {
//        
//        [attString setAttributes:@{NSLinkAttributeName : _messageDic[@"parentid"]} range:[text rangeOfString:secondUserName]];
//    }
//    if ([checkNull(firstUserName) length] != 0) {
//        [attString setAttributes:@{NSLinkAttributeName : _messageDic[@"uid"]} range:[text rangeOfString:firstUserName]];
//    }
//    return attString;
    
    
    
    NSString *text = firstUserName;
    if ([checkNull(secondUserName) length] != 0) {
        text = [text stringByAppendingString:[NSString stringWithFormat:@" 回复 %@", secondUserName]];
    }
    text = [text stringByAppendingString:[NSString stringWithFormat:@"：%@", contentStr]];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
    if ([checkNull(secondUserName) length] != 0) {
        
        [attString setAttributes:@{NSLinkAttributeName : _messageDic[@"parentid"]} range:[text rangeOfString:secondUserName]];
    }
    if ([checkNull(firstUserName) length] != 0) {
        [attString setAttributes:@{NSLinkAttributeName : _messageDic[@"uid"]} range:[text rangeOfString:firstUserName]];
    }
    return attString;
    
    
}

- (NSMutableAttributedString *)getTextByString:(NSString *)str string:(NSString *)redStr {
    
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:str];
    
    NSRange range = [str rangeOfString:redStr];
    // 设置标签文字的属性
    [attrituteString setAttributes:@{NSForegroundColorAttributeName:FDUSERNAME_COLOR,   NSFontAttributeName:[UIFont systemFontOfSize:14 / FDLaOt]} range:range];
    
    return attrituteString;
}

//- (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel {
//    
//    NSLog(@"%@", link.linkValue);
//}

@end
